/**
  ******************************************************************************
  * @file    dev_uart.c
  * @author  Yun
  * @version V3.0.0
  * @date    2025-02-21
  * @brief   UART设备驱动实现
  * @license EULA (End User License Agreement)
  *          Copyright (c) 2025 星瑜科技自动化 Co., Ltd. All rights reserved.
  ******************************************************************************
  */

#include "dev_uart.h"

/*---------------------- 全局变量定义 ----------------------*/
Dev_Uart_Cfg_TypeDef* stDevUartCfgNodeHead; /*!< UART设备链表头指针 */

/*---------------------- 静态函数 ----------------------*/
/**
 * @brief  接收回调函数
 * @param  pvHwCfg: 硬件配置
 * @retval 无
 */
static void Dev_Uart_RxCallback(void* pvHwCfg)
{
    Dev_Uart_Cfg_TypeDef* pstCurrent = stDevUartCfgNodeHead;
    while(pstCurrent != NULL)
    {
        if(pstCurrent->u8InitState != DEV_IS_INIT ||
           pstCurrent->u8RunState != DEV_IDLE) {
            pstCurrent = pstCurrent->pstNext;
            continue;
        }

        DEV_BUSY(pstCurrent);

        if(pstCurrent->pHwCfg == pvHwCfg)
        {
            DEV_PUSH_EVENT(EVENT_DEV_UART_RX, pstCurrent, pstCurrent);
            break;
        }
        pstCurrent = pstCurrent->pstNext;
    }
}

/**
 * @brief  发送回调函数
 * @param  pvHwCfg: 硬件配置
 * @retval 无
 */
static void Dev_Uart_TxCallback(void* pvHwCfg)
{
    Dev_Uart_Cfg_TypeDef* pstCurrent = stDevUartCfgNodeHead;
    while(pstCurrent != NULL)
    {
        if(pstCurrent->u8InitState != DEV_IS_INIT ||
           pstCurrent->u8RunState != DEV_IDLE) {
            pstCurrent = pstCurrent->pstNext;
            continue;
        }

        DEV_BUSY(pstCurrent);

        if(pstCurrent->pHwCfg == pvHwCfg)
        {
            DEV_PUSH_EVENT(EVENT_DEV_UART_TX, pstCurrent, pstCurrent);
        }
        pstCurrent = pstCurrent->pstNext;
    }
}

/*---------------------- 函数实现 ----------------------*/
/**
 * @brief  注册UART设备
 */
uint8_t Dev_Uart_Register(Dev_Uart_Cfg_TypeDef* pstConfig,
                          void* pvHwCfg,
                          Uart_Slow_Interface_TypeDef* pstDevUartInterface)
{
    /* 参数有效性检查 */
    if (!pstConfig || !pvHwCfg || !pstDevUartInterface) {
        return DEV_ERROR;
    }

    DEV_UN_INIT(pstConfig);

    /* 配置设备参数 */
    pstConfig->pHwCfg = pvHwCfg;
    pstConfig->pstDevUartInterface = pstDevUartInterface;

    /* 执行硬件初始化 */
    CHECK_DEV_STATUS(pstConfig->pstDevUartInterface->Init(pvHwCfg,
                                                          pstConfig->pu8RxBuffer,
                                                          pstConfig->u16RxBufSize,
                                                          pstConfig->pu8TxBuffer,
                                                          pstConfig->u16TxBufSize,
                                                          Dev_Uart_RxCallback,
                                                          Dev_Uart_TxCallback,
                                                          pstConfig->u16Timeout));

    /* 链表头部插入 */
    pstConfig->pstNext = stDevUartCfgNodeHead;
    stDevUartCfgNodeHead = pstConfig;

    DEV_INIT(pstConfig);

    return DEV_OK;
}

/**
 * @brief  发送数据
 */
uint8_t Dev_Uart_Transmit(Dev_Uart_Cfg_TypeDef* pstConfig,
                          uint8_t* pu8Data,
                          uint16_t u16Len)
{
    /* 参数有效性检查 */
    if (!pstConfig || !pu8Data || u16Len == 0 ||
    pstConfig->u8InitState != DEV_IS_INIT) {
        return DEV_ERROR;
    }

    CHECK_DEV_ENABLE(pstConfig);
    DEV_BUSY(pstConfig);

    /* 检查发送缓冲区是否足够 */
    if (u16Len > pstConfig->u16TxBufSize) {
        return DEV_ERROR;
    }

    CHECK_DEV_STATUS(pstConfig->pstDevUartInterface->Transmit(pstConfig->pHwCfg,
                                                              pu8Data,
                                                              u16Len));

    return DEV_OK;
}

/**
 * @brief  接收数据
 */
uint8_t Dev_Uart_Receive(Dev_Uart_Cfg_TypeDef* pstConfig,
                         uint8_t* pu8Data,
                         uint16_t* pu16Len)
{
    /* 参数有效性检查 */
    if (!pstConfig || !pu8Data || !pu16Len || pstConfig->u8InitState != DEV_IS_INIT) {
        return DEV_ERROR;
    }

    CHECK_DEV_ENABLE(pstConfig);
    DEV_BUSY(pstConfig);

    CHECK_DEV_STATUS(pstConfig->pstDevUartInterface->Receive(pstConfig->pHwCfg,
                                                             pu8Data,
                                                             pu16Len));

    return DEV_OK;
}


/**
 * @brief  设置波特率
 */
uint8_t Dev_Uart_SetBaudrate(Dev_Uart_Cfg_TypeDef* pstConfig,
                             uint32_t u32Baudrate)
{
    /* 参数有效性检查 */
    if (!pstConfig || u32Baudrate == 0) {
        return DEV_ERROR;
    }

    CHECK_DEV_STATUS(pstConfig->pstDevUartInterface->SetBaudrate(pstConfig->pHwCfg,
                                                                 u32Baudrate));

    return DEV_OK;
}
